<?php
header('Content-Type: text/html; charset=UTF-8');

if(!defined('INC_PATH')){
	define("INC_PATH","../../../");
}
$member_id = $_COOKIE['sav_memberid'];
if($member_id == null || !is_numeric($member_id) || $member_id <=0){
	echo '<data>
		<setVariable type="text" name="error" value="ログイン情報が取れませんでした、もう一度ログインしてください" />
		<phpover />
	</data>';
	exit;
}
require_once(INC_PATH."lib_new/db.php");

$error = "null";

//トランザクション-スタート用
$sql_transaction_begin = "BEGIN;";
//トランザクション-コミット用
$sql_transaction_commit = "COMMIT;";
//トランザクション-戻る用
$sql_transaction_roolback = "ROLLBACK;";


$sql_check = "
SELECT COUNT(0) AS cnt
FROM smd_takarakuji_see
WHERE member_id = $member_id
AND regist_date >= '".date("Y-m-01 00:00:00")."'
and regist_date < '".date("Y-m-01 00:00:00",strtotime("+1 month"))."'
";
$con = con_open();

//トランザクションスタート
$r = pg_query($con, $sql_transaction_begin);
	
$res_check = pg_query($con, $sql_check);
if($res_check){
	$cnt = pg_fetch_result($res_check, "cnt");
	
	if($cnt == 0){
		$sql = "
		INSERT INTO smd_takarakuji_see
		(member_id)VALUES($member_id);
		";
		$res = pg_query($con, $sql);
		if(!$res){
			$error = "登録エラーです";
		}
	}
	
	if($cnt == 0 && $res){
		$sql_prize = " SELECT SUM(pro.point) as point ,SUM(pro.changeTicket) AS ticket ,SUM(pro.gamecoin) AS gamecoin 
			FROM smd_takarakuji_get AS get,
			smd_takarakuji_winproducts as pro 
			WHERE pro.del_flag = 0
			AND get.prize = pro.index
			AND get.member_id = $member_id
			AND date_trunc('month',pro.use_date) = date_trunc('month',now() + '-1 month')
			AND date_trunc('month',get.regist_date) = date_trunc('month',now() + '-1 month')
		";
		$res_prize = pg_query($con, $sql_prize);
		
		if ($res_prize)
		{
			$now = date('m',strtotime('-1 month'));
			$win_point = pg_fetch_result($res_prize, "point");
			$win_ticket = pg_fetch_result($res_prize, "ticket");
			$win_gamecoin = pg_fetch_result($res_prize, "gamecoin");
			if($win_point != null && is_numeric($win_point) && $win_point > 0){
				$sql_getpoint = "
				INSERT INTO getpoint (
				memberid
				,itemid
				,point
				,cause
				,comment
				,clickdate
				,actiondate
				,fixdate
				,fixflg
				,payment
				,registdate
				,gamepoint
				) VALUES (
				$member_id
				,-6
				,$win_point
				,'ドル箱宝くじ（".$now."月分当選）合計獲得ポイント'
				,'takarakuji'
				,now()
				,now()
				,now()
				,1
				,0
				,now()
				,0
				)
				";
				$sql_getpoint = mb_convert_encoding($sql_getpoint,"EUC-JP","UTF-8");
			
				$res_setpoint = pg_query($con,$sql_getpoint);
			}else{
				$res_setpoint = 1;
			}
			if($win_gamecoin != null && is_numeric($win_gamecoin) && $win_gamecoin > 0){
				$sql_getgamecoin = "
				INSERT INTO getpoint (
				memberid
				,itemid
				,point
				,cause
				,comment
				,clickdate
				,actiondate
				,fixdate
				,fixflg
				,payment
				,registdate
				,gamepoint
				) VALUES (
				$member_id
				,-6
				,0
				,'ドル箱宝くじ（".$now."月分当選）合計獲得ゲームコイン'
				,'takarakuji'
				,now()
				,now()
				,now()
				,1
				,0
				,now()
				,".$win_gamecoin."
				)
				";
				$sql_getgamecoin = mb_convert_encoding($sql_getgamecoin,"EUC-JP","UTF-8");
			
				$res_setgamecoin = pg_query($con,$sql_getgamecoin);
			}else{
				$res_setgamecoin = 1;
			}
			if($win_ticket != null && is_numeric($win_ticket) && $win_ticket > 0){
				$sql_getpoint2 = "
				INSERT INTO getpoint (
				memberid
				,itemid
				,point
				,cause
				,comment
				,clickdate
				,actiondate
				,fixdate
				,fixflg
				,payment
				,registdate
				,gamepoint
				) VALUES (
				$member_id
				,-6
				,0
				,'ドル箱宝くじ（".$now."月分当選）合計獲得交換券'
				,'takarakuji'
				,now()
				,now()
				,now()
				,1
				,$win_ticket
				,now()
				,0
				)
				";
				$sql_getpoint2 = mb_convert_encoding($sql_getpoint2,"EUC-JP","UTF-8");
			
				$res_setpoint2 = pg_query($con,$sql_getpoint2);
					if($res_setpoint2){
					$sql_getpoint3 = "UPDATE member SET fukubiki = (fukubiki + ".$win_ticket.")
					WHERE memberid = $member_id ";
					$res_setpoint3 = pg_query($con,$sql_getpoint3);
					if(!$res_setpoint3){
						$res_setpoint2 = 0;
					}
				}
			}else{
				$res_setpoint2 = 1;
			}
			if(!$res_setpoint || !$res_setpoint2){
				$file_log = fopen("./log/error_log.txt","a");
				fwrite($file_log,date ("Y-m-d H:i:s",time())."\n".$member_id."\n".$sql_getpoint."\n".$sql_getpoint2."\n".$sql_getpoint3."\n");
				fclose($file_log);
				$error = "通帳に反映エラーです";
			}else{
				$sql_show = " UPDATE smd_takarakuji_get SET flag = 1 
				WHERE member_id=$member_id
				AND prize > 0 
				AND flag = 0
				AND date_trunc('month',regist_date) = date_trunc('month',now() + '-1 month')";
				$res_show = pg_query($con,$sql_show);
				if(!$res_show){
					$error = "結果反映エラーです";
				}
			}
		}
	}
}else{
	$error = "チェックエラーです";
}
if($error == "null"){
	//エラーがない時、トランザクションコミット
	pg_query($con, $sql_transaction_commit);
}else{
	//エラーが発生する時、処理を戻る
	pg_query($con, $sql_transaction_roolback);
}
con_close($con);

echo '<data>
	<setVariable type="text" name="error" value="'.$error.'" />
	<phpover />
</data>';
?>